import axios from 'axios'
import { message } from 'antd'
import { getToken } from '../utils/user-token'

const instance = axios.create({
  timeout: 10000,

})
// request拦截，每次请求带上token
instance.interceptors.request.use((config) => {
  config.headers['Authorization'] = `Bearer ${getToken()}`
  return config
}, err => {
  Promise.reject(err)
})
instance.interceptors.response.use(res => {
  const resData = (res.data || {}) as ResType
  const { errorno, data = {}, msg } = resData
  if (errorno !== 0) {
    if (msg) {
      message.error(msg)
    }

    throw new Error(msg)
  }
  return data as any
})

export default instance

export type ResType = {
  errorno: number
  data?: ResDataType
  msg?: string
}

export type ResDataType = {
  [key: string]: any
}